<?php


namespace App\Admin\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Request;

/**
 * App\Admin\Model\Diy
 *
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Admin\Model\Diy newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Admin\Model\Diy newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Admin\Model\Diy query()
 * @mixin \Eloquent
 */
class Diy extends Model
{
    public function paginate()
    {
        $perPage = Request::get('per_page', 10);

        $page = Request::get('page', 1);

        $start = ($page-1)*$perPage;

        $data = file_get_contents("https://api.douban.com/v2/movie/in_theaters?city=上海&start=$start&count=$perPage");

        $data = json_decode($data, true);

        extract($data);

        $movies = static::hydrate($subjects);

        $paginator = new LengthAwarePaginator($movies, 2, $perPage);

        $paginator->setPath(url()->current());

        return $paginator;
    }

    public static function with($relations)
    {
        return new static;
    }

    // 覆盖`orderBy`来收集排序的字段和方向
    public function orderBy($column, $direction = 'asc')
    {

    }

    // 覆盖`where`来收集筛选的字段和条件
    public function where($column, $operator = null, $value = null, $boolean = 'and')
    {

    }

}
